package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.CheckInfoResultBean.CheckValue;

import java.net.URLDecoder;
import java.util.ArrayList;

public class OrderItemAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<CheckValue> value;
    private Context context = null;

    public OrderItemAdapter(Context context, ArrayList<CheckValue> value) {
        this.context = context;
        this.value = value;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return value != null ? value.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return value.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            Holder2 holder = new Holder2();
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.order_list_item_lv,
                        null);
            }

            holder.photoImageView = (ImageView) convertView
                    .findViewById(R.id.ouka_order_photo);
            holder.nameTextView = (TextView) convertView
                    .findViewById(R.id.ouka_order_name);
            holder.weightTextView = (TextView) convertView
                    .findViewById(R.id.ouka_order_weight);
            holder.priceTextView = (TextView) convertView
                    .findViewById(R.id.ouka_order_price);
            holder.countTextView = (TextView) convertView
                    .findViewById(R.id.ouka_order_count);

            if (position < value.size()) {
                CheckValue checkValue = value.get(position);
                holder.nameTextView.setText(checkValue.getName());
                holder.priceTextView.setText("￥" + checkValue.getPrice());
                holder.countTextView.setText("×" + checkValue.getCount());
                if (checkValue.getModel() != null) {
                    holder.weightTextView.setText(URLDecoder.decode(checkValue
                            .getModel()));
                } else {
                    holder.weightTextView.setText("");
                }
                MyApplication.loadImageByPicasso(context, checkValue.getImg(),
                        null, holder.photoImageView, 0, 0, 1);
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

}

class Holder2 {
    ImageView photoImageView;
    TextView nameTextView, weightTextView, priceTextView, countTextView;
}
